<?php
require_once 'Encode.php';
require_once 'albumModel.php';
require_once 'DBConnector.php';
require_once 'view.php';
session_start();

$_func=e($_REQUEST['request']);
if ($_func==NULL){
	showAlbumList();
}else{
	$_func();
}

function getJsonPictureData(){
	$result=selectPhoto(e($_REQUEST['AIndex']));
	$jsonData=json_encode($result);
	print_r($jsonData);
}

function selectAlbum($albumIndex=NULL){
	$albumModel = new AlbumModel();
	$db = DBConnector::getDB();
	if($albumIndex==NULL){
		$sth = $db->prepare("select * from tAlbum");
	} else {
		$sth = $db->prepare("select * from tAlbum where AIndex={$albumIndex}");
	}
	return $albumModel->select($sth);
}

function showAlbumList(){
	$result = selectAlbum();
	$view = new View($result);
	$view->render("albumList.php");
}

function insertAlbum(){
	$_userId = $_SESSION['userId'];
	$_albumTitle = e($_REQUEST['ATitle']);
	if (trim($_albumTitle)=='') $_albumTitle = "アルバム・タイトル"; 
	$_albumContent = e($_REQUEST['AContent']);
	if(trim($_albumContent)=='') $_albumContent = "アルバム・内容";
	$albumModel = new AlbumModel();
	$insertResult=$albumModel->insert($_userId,$_albumTitle,$_albumContent);
	if ($insertResult){
		$sth = DBConnector::getDB()->prepare("select max(AIndex) as max from tAlbum");
		$result=$albumModel->select($sth);
		$albumMaxIndex=$result[0]['max'];
		mkdir("../img/".$albumMaxIndex);
	}
	showAlbumList();
}

function updateAlbum(){
	$_aTitle = e($_REQUEST['ATitle']);
	$_aContent = e($_REQUEST['AContent']);
	$_aIndex = e($_REQUEST['AIndex']);
	$sth = DBConnector::getDB()
	->prepare("Update tAlbum SET ATitle=?, AContent=? WHERE AIndex=?");
	$sth->bindParam(1, $_aTitle);
	$sth->bindParam(2, $_aContent);
	$sth->bindParam(3, $_aIndex);
	$sth->execute();
	showPhotoList();
}

function showAlbumEdit(){
	$result=selectAlbum(e($_REQUEST['AIndex']));
	$view = new View($result);
	$view->render("albumEdit.php");
}

function selectPhoto($albumIndex=NULL){
	$albumModel = new AlbumModel();
	$albumModel->setTargetTable(AlbumModel::TABLE_NAME_PICTURE);
	$db = DBConnector::getDB();
	if($albumIndex==NULL){
		$sth = $db->prepare("select * from tPicture");
	} else {
		$sth = $db->prepare("select * from tPicture where AIndex={$albumIndex}");
	}
	return $albumModel->select($sth);
}

function showPhotoList(){
	$_albumIndex = e($_REQUEST['AIndex']);
	$resultAlbum = selectAlbum($_albumIndex);
	$_SESSION['AIndex']=$_albumIndex;
	$_SESSION['ATitle']=e($resultAlbum[0]['ATitle']);
	$_SESSION['AContent']=e($resultAlbum[0]['AContent']);
	$result=selectPhoto($_albumIndex);
	$view = new View($result);
	$view->render("photoList.php");
}

function deletePhoto(){
	if ($_SESSION['token']==e($_REQUEST['token'])) {
		$_pIndex = e($_REQUEST['PIndex']);
		$sth=DBConnector::getDB()->prepare("select PPath from tPicture where PIndex={$_pIndex}");
		$albumModel = new AlbumModel();
		$result=$albumModel->select($sth);
		$albumModel->setTargetTable(AlbumModel::TABLE_NAME_PICTURE);
		$albumModel->delete($_pIndex);
		$_SESSION['token']=NULL;
		unlink("../".$result[0]['PPath']);
		showPhotoList();
	}
}

function showAddAlbum(){
	$view = new View();
	$view->render("addAlbum.php");
}

function insertPhoto(){
	$_albumIndex = e($_REQUEST['AIndex']);
	$src=$_FILES['upfile']['tmp_name'];
	$dest=mb_convert_encoding($_FILES['upfile']['name'], 'SJIS-WIN', 'UTF-8');
	$fileName = 'img/'.$_albumIndex.'/'.date("YmdHis").$dest;
	if (!move_uploaded_file($src,'../'.$fileName)) {
		return false;
	}else {
		$albumModel = new AlbumModel();
		$albumModel->setTargetTable(AlbumModel::TABLE_NAME_PICTURE);
		$albumModel->insert($_SESSION['userId'], $_albumIndex, $fileName, "");
		showPhotoList();
	}
}